package com.zmn.oms.task.job.orderpart;

import com.alibaba.fastjson.JSON;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.google.common.collect.Maps;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.business.interfaces.messageV1.app.MessageSendAppService;
import com.zmn.oms.business.interfaces.work.masterwork.MasterWorkBService;
import com.zmn.oms.common.constant.MessageConsts;
import com.zmn.oms.common.constant.OrderPartConsts;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.model.dto.work.masterwork.MasterPartRetainDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.work.autoflow.WorkFlowContextBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * 类描述：配件保留24小时
 * @author heciqi
 * @date 2021/8/4 11:45
 */
@Slf4j
@LTS
public class OrderPartRetainJob {
    @Autowired
    private MasterWorkBService masterWorkBService;
    @Autowired
    private OrderWorkService orderWorkService;

    @Autowired
    private WorkFlowContextBService workFlowContextBService;
    @Autowired
    private MessageSendAppService messageSendAppService;

    /**
     * 配件保留任务
     * @param job
     * @return
     * @throws Throwable
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_RETAIN_PART)
    public Result runRetainPart(Job job) throws Throwable {
        log.info("OrderPartRetainJob#runRetainPart 执行：" + job);
        Long orderId = Long.valueOf(job.getParam("orderId"));
        Long workId = Long.valueOf(job.getParam("workId"));
        Integer execType = Integer.valueOf(job.getParam("execType"));

        OrderWork orderWork = orderWorkService.findOrderWorkByKeySrcMaster(orderId, workId);
        if (Objects.isNull(orderWork)) {
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        }

        switch (execType.intValue()) {
            // 自动算账
            case OrderPartConsts.RETAIL_PART_24HOUR_AOTUACCOUNT:
                this.autoAccount(orderWork);
                break;
            // 剩余12小时提醒
            case OrderPartConsts.RETAIL_PART_12HOUR_REMIND:
            // 剩余6小时提醒
            case OrderPartConsts.RETAIL_PART_6HOUR_REMIND:
                Map<String, String> params = Maps.newHashMap();
                params.put("order_id", String.valueOf(orderId));
                params.put("work_id", String.valueOf(workId));
                params.put("time", DateUtil.getDateTimeStr(DateUtil.addDays(orderWork.getCompleteTime(), 1)));
                messageSendAppService.pushAppMessageInfo(MessageConsts.APP_MSG_PART_RETAIN_REMIND, JSON.toJSONString(params), orderWork);
                break;
            default:
                log.error("未知执行类型[{}]", execType);
        }
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }

    /**
     * 自动算账
     * @param orderWork
     */
    private void autoAccount(OrderWork orderWork) {
        if (Objects.equals(orderWork.getPartRetain(), GlobalConsts.YES)) {
            // 1，24小时后更新不保留配件，自动流转到收单
            MasterPartRetainDTO masterPartRetainDTO = new MasterPartRetainDTO();
            if (Objects.equals(orderWork.getPartUse(), GlobalConsts.NONE)) {
                masterPartRetainDTO.setPartUse(GlobalConsts.NO);// 更新成未使用配件
            }
            masterPartRetainDTO.setMasterId(orderWork.getMasterId());
            masterPartRetainDTO.setRetainPart(com.zmn.consts.GlobalConsts.NO);
            masterPartRetainDTO.setOrderId(orderWork.getOrderId());
            masterPartRetainDTO.setWorkId(orderWork.getWorkId());
            masterPartRetainDTO.setPlat(orderWork.getPlatWork());
            masterPartRetainDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
            masterPartRetainDTO.setOperatorId(1L);
            masterPartRetainDTO.setOperator("系统");

            masterWorkBService.updatePartRetain(masterPartRetainDTO);
            log.info("orderId:{}，取消保留配件", orderWork.getOrderId());
        }
        // 继续自动流转
        workFlowContextBService.autoFlow(orderWork.getOrderId(), orderWork.getWorkId());
    }
}
